<?php
namespace App\Http\Controllers\v1;

use Mockery\Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\RequestBackController;
use App\Http\Controllers\Controller;
use App\Http\Controllers\CheckController;
use App\Http\Controllers\ToolController;


class CalendarController extends Controller
{
    public function CalendarInfo()
    {
        $date=date('Y-m-d');
        $res=DB::table('yl_calendar')->where('date',$date)->first();
        if(!empty($res))
        {
            $res->content=json_decode($res->content,true);
            return RequestBackController::Structure(101,$res->content);
        }
        else
        {
            $url='https://www.sojson.com/open/api/lunar/json.shtml';
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, false); // 不返回头信息
            curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $content = curl_exec($ch);
            curl_close($ch);
            $content=json_decode($content,true);
            if($content['status']!=200)
            {
                return RequestBackController::Structure(508);
            }
            else
            {
                DB::table('yl_calendar')->insert(['date'=>$date,'content'=>json_encode($content['data'])]);
                return RequestBackController::Structure(101,$content['data']);
            }
        }
    }
}